import numpy as np
import sys
import seaborn as sns
import pylab as plt
from sklearn.neighbors import KernelDensity
import numpy as np
import numpy as np
from matplotlib import pyplot as plt
from sklearn.model_selection import GridSearchCV, LeaveOneOut
from sklearn.neighbors import KernelDensity
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
np.set_printoptions(threshold=sys.maxsize)
columns = 50
rows = 52
numsarray=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 204, 122, 73, 52, 76, 40, 40, 19, 16, 33, 8, 3, 52, 31, 23, 15, 9, 13, 6, 5, 6, 8, 10, 32, 16, 15, 22, 38, 33, 25, 69, 2, 4, 17, 9, 14, 12, 21, 29, 3, 12, 6, 9, 17, 11, 12, 19, 7, 6, 93, 209, 158, 95, 71, 67, 60, 48, 42, 17, 8, 28, 12, 134, 34, 18, 7, 1, 0, 0, 4, 0, 2, 3, 7, 56, 36, 44, 23, 7, 13, 40, 0, 14, 13, 6, 33, 14, 32, 17, 2, 14, 7, 19, 16, 21, 26, 16, 12, 15, 128, 177, 195, 156, 69, 74, 140, 62, 25, 20, 2, 15, 19, 106, 24, 6, 3, 0, 0, 3, 2, 0, 1, 7, 22, 22, 34, 9, 7, 3, 36, 38, 0, 23, 8, 34, 15, 20, 30, 0, 23, 8, 14, 22, 22, 17, 9, 18, 15, 10, 160, 154, 173, 150, 108, 87, 73, 49, 52, 9, 8, 19, 40, 102, 10, 0, 5, 0, 0, 9, 0, 3, 6, 20, 10, 0, 8, 10, 6, 3, 54, 30, 7, 27, 27, 20, 15, 31, 8, 8, 17, 3, 24, 12, 16, 20, 10, 21, 11, 6, 128, 181, 119, 200, 144, 87, 82, 46, 52, 12, 20, 10, 90, 46, 3, 0, 0, 5, 8, 0, 3, 12, 11, 2, 10, 9, 5, 5, 0, 7, 90, 3, 27, 29, 22, 11, 25, 18, 2, 26, 1, 13, 20, 27, 14, 10, 14, 20, 9, 9, 119, 189, 154, 169, 194, 128, 104, 69, 48, 19, 16, 27, 90, 5, 0, 3, 0, 5, 7, 9, 15, 5, 1, 11, 6, 3, 4, 3, 0, 37, 61, 20, 55, 23, 14, 14, 29, 2, 19, 8, 2, 31, 12, 14, 16, 8, 11, 23, 3, 20, 205, 158, 173, 165, 174, 176, 78, 76, 43, 35, 26, 40, 71, 0, 0, 2, 0, 11, 14, 11, 3, 5, 5, 6, 2, 4, 7, 0, 1, 100, 32, 30, 47, 7, 10, 24, 13, 13, 15, 5, 15, 15, 20, 15, 6, 4, 22, 9, 17, 19, 172, 163, 149, 177, 161, 196, 110, 115, 49, 61, 25, 48, 70, 6, 0, 5, 16, 31, 30, 31, 23, 10, 0, 10, 5, 2, 10, 14, 20, 54, 9, 43, 24, 9, 15, 22, 1, 20, 10, 0, 30, 4, 23, 16, 9, 9, 21, 15, 24, 15, 137, 185, 157, 167, 177, 170, 186, 123, 106, 63, 48, 61, 21, 27, 22, 20, 27, 8, 9, 15, 0, 7, 19, 23, 22, 22, 10, 4, 8, 49, 12, 35, 7, 13, 25, 7, 19, 12, 0, 19, 9, 20, 17, 4, 5, 10, 32, 14, 18, 42, 100, 173, 206, 155, 176, 200, 219, 170, 131, 76, 50, 39, 34, 29, 7, 13, 12, 3, 2, 7, 0, 0, 0, 10, 0, 19, 2, 0, 14, 71, 25, 25, 9, 15, 23, 9, 21, 6, 0, 28, 3, 18, 16, 6, 5, 5, 36, 11, 45, 18, 69, 96, 202, 213, 173, 192, 200, 270, 227, 78, 67, 43, 10, 18, 21, 16, 4, 1, 3, 5, 0, 0, 0, 8, 16, 7, 0, 0, 21, 58, 23, 9, 13, 19, 13, 21, 7, 0, 14, 11, 20, 14, 5, 11, 10, 23, 22, 31, 29, 6, 80, 84, 128, 242, 208, 222, 228, 323, 271, 168, 43, 28, 22, 14, 34, 7, 0, 4, 3, 3, 3, 0, 1, 22, 8, 2, 0, 3, 57, 39, 12, 15, 15, 24, 9, 16, 1, 0, 28, 6, 15, 18, 5, 11, 12, 25, 29, 34, 14, 6, 71, 144, 128, 152, 257, 241, 219, 306, 319, 282, 45, 17, 34, 23, 22, 13, 8, 6, 0, 0, 5, 0, 27, 12, 1, 4, 0, 10, 60, 22, 17, 11, 23, 9, 20, 7, 0, 3, 25, 21, 12, 8, 14, 12, 12, 35, 25, 33, 8, 2, 65, 58, 111, 144, 185, 231, 286, 260, 296, 301, 136, 15, 36, 31, 13, 18, 13, 7, 0, 0, 4, 29, 26, 0, 1, 0, 9, 17, 63, 27, 17, 10, 24, 2, 27, 0, 0, 15, 19, 12, 14, 10, 16, 4, 24, 28, 31, 19, 4, 0, 127, 91, 90, 174, 157, 236, 251, 259, 269, 314, 268, 89, 23, 28, 15, 12, 22, 15, 0, 0, 4, 28, 14, 0, 1, 3, 7, 29, 54, 41, 23, 25, 9, 16, 11, 0, 0, 30, 17, 10, 16, 16, 7, 6, 37, 19, 37, 9, 3, 0, 306, 241, 145, 149, 154, 200, 264, 237, 273, 287, 342, 204, 63, 23, 16, 28, 15, 18, 11, 4, 4, 8, 2, 0, 5, 5, 13, 48, 48, 17, 12, 29, 0, 21, 6, 0, 18, 24, 6, 20, 7, 16, 6, 31, 20, 21, 34, 0, 4, 0, 349, 417, 319, 306, 171, 161, 321, 253, 175, 305, 323, 356, 137, 44, 37, 10, 20, 21, 18, 7, 6, 0, 6, 0, 8, 2, 29, 36, 59, 9, 18, 11, 6, 19, 4, 2, 23, 13, 15, 15, 9, 11, 15, 29, 11, 28, 19, 0, 2, 1, 247, 306, 409, 384, 307, 299, 264, 286, 250, 196, 323, 367, 343, 116, 44, 30, 13, 19, 27, 13, 0, 0, 5, 6, 1, 30, 32, 81, 18, 7, 28, 2, 14, 6, 5, 17, 22, 12, 16, 3, 15, 15, 26, 18, 13, 31, 11, 0, 0, 4, 320, 320, 294, 354, 357, 350, 354, 348, 359, 235, 225, 368, 412, 288, 98, 37, 29, 9, 24, 16, 8, 0, 4, 12, 18, 30, 35, 45, 45, 17, 14, 1, 20, 0, 1, 36, 12, 17, 11, 4, 15, 21, 19, 16, 13, 22, 5, 0, 0, 5, 328, 412, 419, 361, 300, 373, 370, 348, 419, 459, 316, 244, 409, 447, 273, 97, 45, 16, 13, 36, 33, 5, 19, 26, 20, 25, 37, 40, 24, 24, 4, 8, 15, 0, 20, 27, 20, 10, 8, 14, 14, 24, 22, 10, 24, 13, 0, 6, 6, 8, 231, 337, 461, 479, 413, 367, 354, 390, 432, 462, 564, 446, 328, 426, 502, 290, 100, 55, 16, 33, 67, 43, 12, 6, 24, 10, 27, 46, 18, 24, 0, 14, 7, 2, 22, 23, 16, 11, 8, 15, 23, 21, 22, 15, 14, 7, 6, 13, 11, 17, 111, 158, 212, 322, 807, 519, 441, 351, 358, 429, 469, 587, 580, 526, 487, 507, 308, 98, 87, 44, 52, 27, 12, 10, 28, 32, 18, 29, 13, 17, 3, 19, 2, 15, 29, 6, 22, 9, 23, 14, 20, 9, 14, 27, 22, 13, 9, 24, 24, 21, 101, 180, 200, 196, 312, 417, 459, 522, 396, 392, 464, 469, 575, 589, 653, 632, 560, 391, 150, 119, 29, 15, 28, 27, 23, 45, 31, 28, 33, 7, 18, 22, 2, 30, 18, 14, 12, 57, 6, 18, 13, 12, 17, 21, 21, 26, 19, 20, 23, 40, 121, 129, 159, 193, 258, 293, 384, 459, 550, 496, 403, 470, 518, 640, 650, 679, 789, 699, 558, 264, 99, 25, 16, 55, 16, 47, 38, 16, 22, 16, 2, 20, 18, 24, 11, 22, 13, 22, 15, 15, 12, 15, 38, 29, 7, 13, 22, 26, 76, 53, 93, 136, 108, 140, 175, 208, 281, 411, 559, 612, 646, 566, 493, 468, 554, 575, 674, 882, 972, 744, 338, 146, 44, 75, 41, 45, 10, 22, 18, 99, 12, 14, 22, 18, 18, 13, 33, 6, 6, 15, 34, 50, 25, 10, 12, 40, 48, 74, 53, 101, 95, 79, 73, 102, 182, 177, 144, 243, 279, 486, 603, 696, 654, 527, 413, 498, 558, 602, 825, 1074, 1022, 585, 254, 102, 46, 36, 18, 30, 15, 39, 24, 17, 21, 24, 27, 31, 7, 4, 24, 40, 32, 32, 31, 34, 73, 73, 80, 132, 108, 92, 66, 66, 81, 100, 100, 104, 117, 155, 235, 255, 413, 601, 753, 783, 731, 581, 490, 537, 542, 594, 974, 1264, 985, 489, 149, 44, 41, 35, 21, 32, 22, 22, 17, 39, 28, 7, 22, 38, 29, 25, 47, 38, 56, 64, 48, 67, 50, 85, 107, 106, 79, 69, 81, 105, 156, 128, 175, 183, 186, 301, 324, 400, 584, 789, 1041, 936, 809, 613, 542, 462, 427, 726, 1374, 1484, 853, 308, 125, 50, 26, 43, 20, 45, 47, 32, 24, 27, 29, 20, 33, 44, 55, 78, 43, 62, 70, 70, 98, 99, 96, 147, 87, 36, 105, 155, 228, 176, 185, 234, 256, 204, 217, 248, 296, 471, 813, 1245, 1321, 1123, 744, 527, 404, 343, 474, 1010, 1848, 1658, 706, 252, 138, 52, 76, 53, 89, 39, 59, 61, 50, 59, 64, 61, 96, 110, 98, 64, 79, 130, 133, 136, 209, 155, 56, 95, 156, 156, 157, 196, 154, 179, 182, 185, 183, 183, 251, 310, 342, 489, 971, 1475, 1479, 1131, 665, 429, 322, 324, 575, 1468, 2407, 1719, 678, 235, 98, 88, 51, 62, 55, 73, 66, 80, 111, 136, 136, 90, 83, 90, 133, 129, 172, 197, 183, 166, 115, 111, 138, 162, 190, 168, 185, 176, 139, 172, 185, 225, 243, 195, 143, 206, 304, 558, 1147, 1748, 1745, 1165, 604, 341, 323, 431, 855, 1934, 2630, 1628, 584, 226, 176, 139, 119, 113, 103, 128, 133, 133, 111, 114, 120, 141, 144, 170, 167, 204, 190, 150, 86, 182, 173, 220, 179, 167, 174, 142, 153, 160, 181, 222, 212, 237, 252, 230, 289, 343, 389, 813, 1577, 2255, 1932, 862, 333, 156, 127, 239, 767, 2131, 2431, 1438, 437, 180, 171, 158, 145, 171, 179, 142, 151, 147, 129, 145, 157, 219, 222, 222, 172, 199, 154, 176, 174, 148, 158, 147, 130, 147, 152, 163, 187, 215, 184, 512, 172, 237, 209, 240, 238, 214, 308, 822, 1893, 2810, 1925, 565, 192, 160, 179, 377, 1111, 1980, 2038, 1252, 473, 257, 215, 144, 155, 160, 191, 216, 301, 358, 405, 420, 377, 366, 337, 291, 117, 132, 159, 114, 116, 153, 112, 130, 138, 186, 231, 169, 154, 162, 141, 197, 226, 159, 139, 168, 158, 191, 271, 797, 2190, 3013, 1771, 766, 408, 336, 387, 719, 1375, 1909, 1865, 1199, 602, 497, 508, 463, 408, 401, 399, 325, 320, 296, 257, 240, 246, 214, 95, 103, 140, 120, 146, 101, 103, 135, 187, 195, 147, 146, 141, 133, 159, 166, 172, 123, 131, 146, 265, 263, 317, 363, 508, 1305, 2661, 2965, 1739, 1000, 710, 672, 699, 979, 1448, 1705, 1478, 960, 419, 275, 342, 340, 334, 401, 419, 402, 352, 376, 301, 229, 88, 122, 151, 121, 102, 86, 124, 169, 185, 156, 147, 147, 144, 159, 127, 146, 67, 79, 112, 246, 310, 261, 228, 229, 258, 366, 532, 1234, 2388, 2237, 1372, 895, 741, 746, 903, 1180, 1531, 1542, 1463, 1159, 742, 598, 622, 550, 521, 471, 408, 359, 334, 315, 117, 120, 108, 93, 73, 133, 174, 174, 160, 158, 160, 106, 118, 78, 90, 80, 86, 144, 211, 219, 151, 115, 139, 100, 40, 68, 23, 129, 489, 1309, 1890, 1599, 1019, 831, 774, 803, 1002, 1245, 1385, 1272, 1179, 1048, 835, 646, 557, 557, 518, 509, 453, 352, 83, 75, 90, 94, 111, 170, 155, 161, 158, 122, 86, 106, 62, 85, 75, 92, 128, 181, 136, 148, 69, 94, 39, 31, 87, 26, 114, 74, 108, 170, 631, 1293, 1517, 1164, 797, 723, 677, 933, 1034, 1271, 1093, 892, 752, 797, 726, 652, 632, 598, 534, 427, 62, 92, 84, 97, 147, 166, 152, 154, 104, 83, 92, 69, 69, 80, 100, 100, 115, 102, 82, 50, 56, 31, 32, 21, 31, 46, 98, 30, 46, 20, 77, 288, 791, 1105, 1177, 900, 740, 688, 853, 918, 1119, 1104, 950, 711, 626, 601, 584, 581, 591, 530, 43, 72, 105, 151, 140, 136, 163, 96, 74, 76, 59, 63, 87, 94, 86, 77, 99, 56, 49, 68, 25, 33, 23, 27, 15, 36, 21, 13, 55, 26, 29, 32, 120, 478, 805, 924, 894, 665, 524, 604, 689, 855, 897, 746, 559, 399, 377, 399, 398, 288, 47, 90, 115, 134, 131, 137, 74, 69, 72, 52, 75, 85, 74, 90, 67, 69, 41, 44, 33, 68, 21, 27, 28, 36, 41, 20, 7, 15, 23, 9, 42, 37, 27, 49, 203, 573, 775, 827, 729, 543, 528, 533, 574, 655, 594, 457, 287, 181, 162, 142, 50, 90, 134, 114, 131, 71, 56, 74, 63, 87, 90, 79, 61, 64, 57, 27, 34, 36, 20, 23, 23, 15, 31, 16, 33, 13, 10, 28, 4, 5, 17, 8, 27, 50, 49, 143, 477, 540, 711, 719, 563, 495, 514, 505, 540, 540, 389, 246, 161, 103, 59, 111, 108, 116, 85, 69, 89, 65, 76, 86, 71, 50, 62, 34, 34, 27, 33, 14, 5, 15, 22, 16, 21, 6, 38, 16, 12, 23, 2, 12, 3, 7, 10, 13, 46, 53, 150, 210, 354, 512, 703, 666, 614, 528, 479, 479, 514, 395, 202, 112, 101, 81, 118, 96, 67, 79, 56, 71, 90, 64, 27, 40, 24, 44, 22, 29, 16, 5, 9, 24, 12, 6, 15, 18, 27, 8, 28, 11, 5, 11, 0, 0, 5, 13, 7, 25, 34, 56, 128, 261, 340, 542, 667, 692, 583, 471, 447, 462, 408, 188, 93, 101, 101, 57, 85, 68, 69, 71, 48, 33, 36, 20, 48, 13, 26, 15, 14, 14, 15, 16, 4, 22, 2, 40, 13, 11, 22, 16, 8, 2, 0, 3, 2, 0, 7, 8, 14, 27, 36, 88, 177, 271, 409, 610, 729, 715, 530, 485, 467, 384, 87, 89, 59, 85, 65, 72, 60, 43, 28, 20, 26, 45, 12, 16, 9, 23, 16, 10, 12, 9, 3, 18, 5, 39, 10, 21, 18, 7, 6, 0, 1, 3, 0, 0, 0, 2, 9, 17, 24, 31, 51, 150, 219, 302, 545, 680, 735, 631, 560, 408, 53, 55, 88, 62, 60, 51, 35, 23, 18, 25, 24, 25, 8, 11, 15, 31, 9, 7, 5, 10, 14, 7, 8, 35, 11, 15, 19, 12, 3, 1, 4, 0, 0, 0, 0, 0, 0, 5, 16, 18, 36, 28, 100, 160, 225, 408, 653, 680, 648, 633, 37, 67, 58, 48, 37, 26, 19, 28, 14, 17, 20, 7, 6, 16, 23, 15, 5, 5, 14, 0, 20, 0, 11, 38, 23, 10, 16, 9, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 4, 12, 20, 32, 22, 77, 112, 214, 341, 633, 633, 642, 25, 58, 36, 27, 18, 9, 30, 8, 18, 17, 6, 5, 18, 14, 18, 4, 2, 6, 14, 6, 14, 12, 11, 40, 13, 16, 18, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 20, 31, 24, 48, 86, 143, 250, 474, 598, 48, 26, 6, 16, 11, 7, 3, 19, 10, 9, 7, 11, 11, 6, 11, 1, 0, 9, 4, 8, 0, 8, 21, 17, 4, 14, 25, 34, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 8, 26, 32, 27, 22, 50, 113, 225, 353, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def switchArray(numsarray):
    matrix = np.zeros((rows, columns), dtype=np.int_)
    length=len(numsarray)
    for i in range(length):
        num=numsarray[i]
        # 一维数组nums中的第index个除三取余数为二维数组的行
        a=i // columns
        # 一维数组nums中的第index个除三取模为二维数组的列
        b=i % columns
        matrix[a][b] =num
    return matrix
def get_kde(numsarray):
    matrix = switchArray(numsarray)
    for number in range(columns):
        numslist = []
        column = matrix[:, number]
        for j in range(rows):
            nums = column[j]
            while nums != 0:
                numslist.append(j)
                nums = nums - 1
        X = np.array(numslist)
        x_train = X
        bandwidth = 1.0
        model = KernelDensity(bandwidth=bandwidth, kernel='gaussian')
        model.fit(x_train[:, np.newaxis])
        x_range = np.linspace(x_train.min() - 1, x_train.max() + 1, 52)
        x_log_prob = model.score_samples(x_range[:, np.newaxis])  # 这个东西返回概率的对数
        x_prob = np.exp(x_log_prob)
        x_prob = np.around(x_prob, decimals=3, out=None)
        plt.figure(figsize=(10, 10))
        r = plt.hist(
            x=x_train,
            bins=52,
            density=True,
            histtype='stepfilled',
            color='red',
            alpha=0.5,
            label='histogram',
        )
        plt.fill_between(
            x=x_range,
            y1=x_prob,
            y2=0,
            color='green',
            alpha=0.5,
            label='KDE',
        )
        plt.plot(x_range, x_prob, color='gray')
        # plt.vlines(x=mid, ymin=0, ymax=r[0].max() + 0.01, color='k', linestyle='--', alpha=0.7)  # 分布中心
        plt.ylim(0, r[0].max() + 0.011)
        plt.legend(loc='upper right')
        plt.title(number)
        plt.savefig(r'D:\workspace\data\fig\kde1\{}.png'.format(number))
        plt.show()
get_kde(numsarray)